package com.newbee.model;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
 * 链路上下文模型
 */
public class TraceContext {

    // 业务唯一traceId（不是请求唯一）
    private final String traceId;

    // 仅用于异步线程的场景, 记录根节点id
    private String asyncRootNodeId;

    public TraceContext(String traceId) {
        this(traceId, null);
    }

    public TraceContext(String traceId, String asyncRootNodeId) {
        if (null == traceId || traceId.trim().isEmpty()) {
            throw new IllegalArgumentException("TraceContext初始化失败, 参数traceId是空的");
        }
        this.traceId = traceId;
        this.asyncRootNodeId = asyncRootNodeId;
    }

    @Nonnull
    public String getTraceId() {
        return traceId;
    }

    @Nullable
    public String getAsyncRootNodeId() {
        return asyncRootNodeId;
    }

    public void clearAsyncRootNodeId() {
        this.asyncRootNodeId = null;
    }
}
